package co.sanduche.behave.presenters;

import java.util.List;

import co.sanduche.behave.mmos.AcudienteMMO;
import co.sanduche.behave.mmos.AuthenticationMMO;
import co.sanduche.behave.views.LoginView;
import co.sanduche.models.Models.Estudiante;
import co.sanduche.util.EPCallback;
import co.sanduche.vos.LoginCredentials;

public class LoginPresenter {

	private AuthenticationMMO authenticationMMO;
	private LoginView loginView;
	private AcudienteMMO acudienteMMO;

	public LoginPresenter(LoginView loginFragment) {

		authenticationMMO = new AuthenticationMMO();
		acudienteMMO = new AcudienteMMO();
		loginView = loginFragment;
	}

	public void login(LoginCredentials credentials) {
		authenticationMMO.login(new LoginCredentials() {
			{
				username = "acu1";
				password = "123";
				rememberMe = true;
				role = "ACUDIENTE";
			}
		}, new EPCallback<Void>() {

			@Override
			public void onSuccess(Void val) {

				acudienteMMO.getEstudiantes(new EPCallback<List<Estudiante>>() {

					@Override
					public void onSuccess(List<Estudiante> val) {

						if (val.size() == 1) {
							loginView.goToStudentActivity(val.get(0).getId());
						} else {
							loginView.goToStudentsActivity();
						}

					}

					@Override
					public void onFailure(Throwable e) {
						// TODO Auto-generated method stub

					}
				});

			}

			@Override
			public void onFailure(Throwable e) {
				// TODO Auto-generated method stub
				System.out.println("DAMN");
			}

		});

	}

}
